<?php

namespace Beike\Seller\Providers;

use Beike\Admin\View\Components\Alert;
use Beike\Admin\View\Components\Filter;
use Beike\Admin\View\Components\Form\Image;
use Beike\Admin\View\Components\Form\Input;
use Beike\Seller\Http\Controllers\Components\InputLocale;
use Beike\Admin\View\Components\Form\RichText;
use Beike\Admin\View\Components\Form\Select;
use Beike\Admin\View\Components\Form\SwitchRadio;
use Beike\Admin\View\Components\Form\Textarea;
use Beike\Admin\View\Components\NoData;
use Beike\Seller\Http\Controllers\Components\Sidebar;
use Beike\Seller\Models\SellerUser;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
require_once __DIR__ . "/../Helpers.php";

class SellerServiceProvider extends ServiceProvider
{
    /**
     * @throws \Exception
     */
    public function boot()
    {
        $this->loadRoutesFrom(__DIR__ . '/../Routes/seller.php');

        if (is_installer()) {
            return;
        }

        Schema::defaultStringLength(191);

        $this->registerFileSystem();
        $this->mergeConfigFrom(__DIR__ . '/../config.php', 'seller');
        $this->mergeConfigFrom(__DIR__ . '/../../Config/beike.php', 'beike');
        $this->loadViewsFrom(__DIR__ . '/../Views', 'seller');
        $this->loadViewsFrom(resource_path('/beike/admin/views'), 'admin');
        $this->app->booted(function () {
            View::share('seller_base_url', seller_route('home.index'));
        });
        $this->loadViewComponents();
        $this->registerGuard();

        $pathSeller = base_path('beike/Seller');
        $this->loadTranslationsFrom("{$pathSeller}/Lang", 'seller');
    }

    protected function loadViewComponents()
    {
        $this->loadViewComponentsAs('seller', [
            'sidebar'           => Sidebar::class,
            'filter'            => Filter::class,
            'alert'             => Alert::class,
            'form-input-locale' => InputLocale::class,
            'form-switch'       => SwitchRadio::class,
            'form-input'        => Input::class,
            'form-select'       => Select::class,
            'form-image'        => Image::class,
            'form-textarea'     => Textarea::class,
            'form-rich-text'    => RichText::class,
            'no-data'           => NoData::class,
        ]);
    }

    /**
     * 注册上传文件系统
     */
    protected function registerFileSystem()
    {

        Config::set('filesystems.disks.catalog', [
            'driver' => 'local',
            'root'   => public_path('catalog'),
        ]);
    }

    /**
     * 注册后台用户 guard
     */
    protected function registerGuard()
    {
        Config::set('auth.guards.' . SellerUser::AUTH_GUARD, [
            'driver'   => 'session',
            'provider' => 'seller_users',
        ]);

        Config::set('auth.providers.seller_users', [
            'driver' => 'eloquent',
            'model'  => SellerUser::class,
        ]);
    }
}
